const app = getApp()
const util = require("../../utils/util.js")

Page({
  data: {
    iconType: ['search'],
    iconColor: ['balck'],
    banner: [],//广告位数据轮播图
    channel: [],//频道数据
    newGoodsList: [],//新品列表
    hotGoodsList: [],//热卖列表
    brandList: [],//品牌列表
    topicList: [],//话题列表
    categoryList: [],//分类
    goodsList: [],
    idx:0//首页刷新第一个激活
  },

  //转跳搜索界面
  search() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
    userInfo: { }
    userInfo: { }
    hasUserInfo: false
  },

  //轮播图转跳
  banner(e) {
    console.log('banner', e)
  },

  //点击分类传对应index渲染数据
  categoryList(e) {
    console.log(e)
    var idx = e.target.dataset.idx
    // console.log(idx)
    this.setData({
      goodsList: this.data.categoryList[idx].goodsList,
      idx: idx
    })
  },

  //分类的商品
  list(e) {
    var id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?id=' + id,
      event: {
        // 给goodsCategory页面发送categoryID
        sendcategoryId(data) {
          console.log("Data", data);
        },
      },
      success: function (res) {
        res.eventChannel.emit('sendcategoryId', { data: id })
      }
    })
  },

  //热门商品
  hotlist(e) {
    var id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?id=' + id,
      event: {
        // 给goodsCategory页面发送categoryID
        sendcategoryId(data) {
          console.log("Data", data);
        },
      },
      success: function (res) {
        res.eventChannel.emit('sendcategoryId', { data: id })
      }
    })
  },

  //新品上市
  newlist(e) {
    var id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?id=' + id,
      event: {
        // 给goodsCategory页面发送categoryID
        sendcategoryId(data) {
          console.log("Data", data);
        },
      },
      success: function (res) {
        res.eventChannel.emit('sendcategoryId', { data: id })
      }
    })
  },


  //更多制造商
  morebrand() {
    wx.navigateTo({
      url: '/pages/indexSpace/brandList/brandList',
    })
  },
  //制造商详情
  brandList(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/indexSpace/brandDetail/brandDetail?id=' + id,
    })
  },

  //更多新品
  moreNew() {
    wx.navigateTo({
      url: '/pages/indexSpace/goodsNew/goodsNew',
    })
  },
  //更多热门
  moreHot() {
    wx.navigateTo({
      url: '/pages/indexSpace/goodsHot/goodsHot',
    })
  },
  //更多话题
  moreTop() {
    wx.navigateTo({
      url: '/pages/topiclist/topiclist',
    })
  },
  //话题详情
  tolist(e) {
    var id = e.currentTarget.dataset.id
    console.log(e)
    wx.navigateTo({
      url: '/pages/topicDetail/topicDetail?id=' + id,
    })
  },

  onLoad: function () {
    // this.myfunction()
    let self = this;//改变this指向
    wx.request({//发起 HTTPS 网络请求
      url: util.api.index,
      success: function (res) {
        console.log(res)
        self.setData({
          banner: res.data.data.banner,
          brandList: res.data.data.brandList,
          categoryList: res.data.data.categoryList,
          channel: res.data.data.channel,
          hotGoodsList: res.data.data.hotGoodsList,
          newGoodsList: res.data.data.newGoodsList,
          topicList: res.data.data.topicList,
          goodsList: res.data.data.categoryList[0].goodsList//刷新的第一个显示内容
        })
      }
    })
    /* home page */
    // myfunction:function(){ //util.js封装函数，创建函数，在生命周期函数调用
    // util.myRequest({
    //   url: util.api.index,
    //   success: function (res) {
    //     console.log('index', res)
    //   }
    // })
    // },

    /* goods detail */
    util.myRequest({
      url: util.api.goodsDetail,
      method: 'get',
      data: { id: 1116011 },
      success: function (res) {
        console.log('goodsDetail', res)
      }
    })
  },

})
